package com.cnit1818.mybatis.jdbc;

import com.cnit1818.mybatis.dialect.Dialect;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

/**
 * 数据库操作JdbcDao实现
 *
 */
@SuppressWarnings("unchecked")
public class JdbcDao extends SimpleJdbcTemplate implements InitializingBean
{
    private Dialect dialect;

    public JdbcDao(DataSource dataSource)
    {
        super(dataSource);
    }

    public List<Map<String, Object>> queryForList(String sql, int skipResults, int maxResults, Map args) throws DataAccessException
    {
        String limitSql = dialect.getLimitString(sql, skipResults, maxResults);
        return queryForList(limitSql, args);
    }

    public List<Map<String, Object>> queryForList(String sql, int skipResults, int maxResults, SqlParameterSource args) throws DataAccessException
    {
        String limitSql = dialect.getLimitString(sql, skipResults, maxResults);
        return queryForList(limitSql, args);
    }

    public List<Map<String, Object>> queryForList(String sql, int skipResults, int maxResults, Object... args) throws DataAccessException
    {
        String limitSql = dialect.getLimitString(sql, skipResults, maxResults);
        return queryForList(limitSql, args);
    }

    public Dialect getDialect()
    {
        return dialect;
    }

    public void setDialect(Dialect dialect)
    {
        this.dialect = dialect;
    }

    @Override
    public void afterPropertiesSet() throws Exception
    {
        //
    }
}
